package com.bdc.whatwhat.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.bdc.whatwhat.R;
import com.bdc.whatwhat.fragments.ConnexionFragment;
import com.bdc.whatwhat.notification.GcmInitializer;
import com.bdc.whatwhat.session.SessionManager;

/**
 * Created by gfimac on 20/06/2014.
 */
public class ConnexionActivity extends SherlockFragmentActivity implements GcmInitializer.GcmInitialiserCallBack {

    Fragment fragment;
    SessionManager sessionManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connexion);

        sessionManager = new SessionManager(this);
        if(sessionManager.getProfil() != null){
            startActivity(new Intent(this,MainActivity.class));
            overridePendingTransition(0,0);
            finish();
        }else {

            // Si l'utilisateur n'est pas encore inscrit auprès du serveur de notification
            if( sessionManager.getJeton() == null) {
                new GcmInitializer(this, this).enregistrerCloudMessaginClient();
            }else
            {
                Log.d("PLAY", sessionManager.getJeton());
            }


            fragment = new ConnexionFragment();
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_connexion_holder, fragment).commit();
        }
    }

    @Override
    public void onGcmInitialised(String token) {
        sessionManager.setJeton(token);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(fragment != null)
            fragment.onActivityResult(requestCode,resultCode,data);
    }
}
